//
//  RobController.m
//  MyTrip
//
//  Created by zhang on 15/8/27.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "RobController.h"
#import "DOPDropDownMenu.h"
#import "AsyncGETConnection.h"
#import "DiscountCell.h"
#import "SlideController.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"

@interface RobController ()<DOPDropDownMenuDataSource, DOPDropDownMenuDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *collectionArr;
@property (nonatomic, retain) NSArray *typeArr;
@property (nonatomic, retain) NSArray *startArr;
@property (nonatomic, retain) NSArray *timeArr;
@property (nonatomic, retain) NSMutableDictionary *parameters;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation RobController

- (void)dealloc
{
    [_collectionArr release];
    [_collection release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.collectionArr = [NSMutableArray array];
        self.parameters = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"0", @"product_type", @"", @"departure", @"", @"times",  nil];
    }
    return self;
}

#pragma mark - <网络请求>
- (void)getMessage{
    self.hud = [[MBProgressHUD alloc] init];
    [self.view addSubview:_hud];
    [_hud show:YES];
    _hud.labelText = @"正在拼命加载......";
    //    在网络请求之后
    
    __block MBProgressHUD *hud = _hud;
    [AsyncGETConnection asyncGETConnectionWithURLStr:@"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&is_show_pay=1&is_show_supplier=1&max_id=0&page=1&page_size=20&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C1&track_deviceid=216B2645-C28F-42CC-A74E-17449CA8DF1A&track_os=ios%25208.1.3&v=1" parameters:_parameters messageBlock:^(NSData *data) {
        hud.hidden = YES;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSArray *lastminutes = [dataDic objectForKey:@"lastminutes"];
        [_collectionArr addObjectsFromArray:lastminutes];
        [_collection reloadData];
    }];
    
    
}

#pragma mark - <collectionview>
- (void)createCollection{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 10 * OffWidth, 250 * OffHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(8 * OffHeight, 7 * OffWidth, 0, 7 * OffWidth);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 30 * OffHeight, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout] autorelease];
    [_collection setBackgroundColor:[UIColor whiteColor]];
    _collection.delegate = self;
    _collection.dataSource = self;
    [_collection registerClass:[DiscountCell class] forCellWithReuseIdentifier:@"discount"];
    [self.view addSubview:_collection];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _collectionArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DiscountCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discount" forIndexPath:indexPath];
    NSDictionary *dic = [_collectionArr objectAtIndex:indexPath.item];
    [cell setValueForCellWithDic:dic];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [_collectionArr objectAtIndex:indexPath.item];
    NSNumber *idNum = [dic objectForKey:@"id"];
    NSString *url = [NSString stringWithFormat:@"http://z.qyer.com/deal/%@/", idNum];
    SlideController *slide = [[SlideController alloc] init];
    slide.url = url;
    [self.navigationController pushViewController:slide animated:YES];
    [slide release];

}





#pragma mark - <dopmenu菜单>
- (void)crateMenu{
    self.typeArr = @[@"全部类型", @"机票", @"酒店", @"自由行", @"邮轮", @"租车", @"保险", @"签证", @"城市玩乐"];
    self.startArr = @[@"全部出发地", @"北京/天津", @"上海/杭州", @"成都/重庆", @"广州/深圳", @"港澳台", @"国内其他", @"海外"];
    self.timeArr = @[@"全部时间", @"1个月内", @"1-3个月", @"3-6个月", @"6个月后"];
    
    CGFloat y = 64 * OffHeight;
    if ([UIScreen mainScreen].bounds.size.height == 736) {
        y = 60;
    }
    
    DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, y) andHeight:40 * OffHeight];
    menu.delegate = self;
    menu.dataSource = self;
    
    [self.view addSubview:menu];
    
}

- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu{
    return 3;
}

- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column{
    if (column == 0) {
        return _typeArr.count;
    }
    if (column == 1) {
        return _startArr.count;
    }
    if (column == 2) {
        return _timeArr.count;
    }
    return 0;
}


- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath{
    switch (indexPath.column) {
        case 0:
            return self.typeArr[indexPath.row];
            break;
        case 1:
            return self.startArr[indexPath.row];
            break;
        case 2:
            return self.timeArr[indexPath.row];
            break;
        default:
            return nil;
            break;
    }
}

- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath{
    if (indexPath.column == 0) {
                switch (indexPath.row) {
            case 0:
                [_parameters setObject:@"0" forKey:@"product_type"];
                break;
            case 1:
                [_parameters setObject:@"1016" forKey:@"product_type"];
                
                break;
            case 2:
                [_parameters setObject:@"1017" forKey:@"product_type"];
                
                break;
            case 3:
                [_parameters setObject:@"1018" forKey:@"product_type"];
                
                break;
            case 4:
                [_parameters setObject:@"1020" forKey:@"product_type"];
                
                break;
            case 5:
                [_parameters setObject:@"1021" forKey:@"product_type"];
                break;
            case 6:
                [_parameters setObject:@"1049" forKey:@"product_type"];
                break;
            case 7:
                [_parameters setObject:@"1785" forKey:@"product_type"];
                break;
            case 8:
                [_parameters setObject:@"2410" forKey:@"product_type"];
                break;
            default:
                break;
        }
    }else if (indexPath.column == 1){
        switch (indexPath.row) {
            case 0:
                [_parameters setObject:@"" forKey:@"departure"];
                break;
            case 1:
                [_parameters setObject:@"bjtj" forKey:@"departure"];
                
                break;
            case 2:
                [_parameters setObject:@"shhz" forKey:@"departure"];
                break;
            case 3:
                [_parameters setObject:@"cdcq" forKey:@"departure"];
                
                break;
            case 4:
                [_parameters setObject:@"gzsz" forKey:@"departure"];
                
                break;
            case 5:
                [_parameters setObject:@"dmt" forKey:@"departure"];
                break;
            case 6:
                [_parameters setObject:@"inland" forKey:@"departure"];
                break;
            case 7:
                [_parameters setObject:@"abroad" forKey:@"departure"];
                break;
            default:
                break;
        }
    }else if (indexPath.column == 2){
        switch (indexPath.row) {
            case 0:
                [_parameters setObject:@"" forKey:@"times"];
                break;
            case 1:
                [_parameters setObject:@"1" forKey:@"times"];
                break;
            case 2:
                [_parameters setObject:@"2" forKey:@"times"];
                break;
            case 3:
                [_parameters setObject:@"3" forKey:@"times"];
                break;
            case 4:
                [_parameters setObject:@"4" forKey:@"times"];
                break;
            default:
                break;
        }
    }
    _hud.hidden = NO;
        [AsyncGETConnection asyncGETConnectionWithURLStr:@"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&is_show_pay=1&is_show_supplier=1&max_id=0&page_size=20&track_app_channel=App%2520Store&track_app_version=6.5.5&track_device_info=iPhone7%2C1&track_deviceid=216B2645-C28F-42CC-A74E-17449CA8DF1A&track_os=ios%25208.1.3&v=1" parameters:_parameters messageBlock:^(NSData *data) {
            _hud.hidden = YES;
            [_collectionArr removeAllObjects];
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            NSArray *lastminutes = [dataDic objectForKey:@"lastminutes"];
            [_collectionArr addObjectsFromArray:lastminutes];
            [_collection reloadData];
        }];
    
}








- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollection];
    [self crateMenu];
    [self getMessage];
    [_collection addFooterWithCallback:^{
        int page = 2;
        _hud.hidden = NO;
        [AsyncGETConnection asyncGETConnectionWithURLStr:[NSString stringWithFormat:@"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&is_show_pay=1&is_show_supplier=1&max_id=0&page=%d&page_size=20%@&track_app_version=6.5.5%@&track_deviceid=216B2645-C28F-42CC-A74E-17449CA8DF1A%@&v=1", page, @"&track_app_channel=App%2520Store", @"&track_device_info=iPhone7%2C1", @"&track_os=ios%25208.1.3"] parameters:_parameters messageBlock:^(NSData *data) {
            _hud.hidden = YES;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            NSArray *lastminutes = [dataDic objectForKey:@"lastminutes"];
            [_collectionArr addObjectsFromArray:lastminutes];
            [_collection reloadData];
            [_collection footerEndRefreshing];
        }];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
